package com.business_clean.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.business_clean.app.App
import com.business_clean.app.network.NetUrl
import com.business_clean.data.mode.attendance.AttendanceEntity
import com.business_clean.data.mode.project.ProjectManager
import com.business_clean.data.mode.workbench.HomeEntity
import com.business_clean.data.mode.workbench.WorkBenchEntity
import com.kunminx.architecture.ui.callback.UnPeekLiveData
import me.hgj.mvvmhelper.base.BaseViewModel
import me.hgj.mvvmhelper.ext.rxHttpRequest
import me.hgj.mvvmhelper.net.LoadingType
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * 首页的Model
 */
class HomeVideModel : BaseViewModel() {


    var homeEntity = MutableLiveData<HomeEntity>()

    var workBenchEntity = MutableLiveData<WorkBenchEntity>()

    var allProjectManager = UnPeekLiveData<ProjectManager>()


    var attendanceEntity = MutableLiveData<AttendanceEntity>()


    /**
     * 获取统计
     */
    fun requestWorkbenchStat(hideDialog: Boolean) {
        rxHttpRequest {
            onRequest = {
                workBenchEntity.value = RxHttp.get(NetUrl.GET_WORK_BENCH_TO_GRID)
                    .add("project_uuid", App.appViewModelInstance.projectInfo.value?.uuid)
                    .toResponse<WorkBenchEntity>().await()
            }
            loadingType = if (hideDialog) LoadingType.LOADING_NULL else LoadingType.LOADING_CUSTOM
        }
    }

    /**
     * 获取grid
     */
    fun requestWorkbenchGrid(hideDialog: Boolean) {
        rxHttpRequest {
            onRequest = {
                homeEntity.value = RxHttp.get(NetUrl.GET_WORK_BENCH_GRID)
                    .toResponse<HomeEntity>().await()
            }
            loadingType = if (hideDialog) LoadingType.LOADING_NULL else LoadingType.LOADING_CUSTOM
        }
    }

    /**
     * 获取所有的项目列表
     */
    fun requestProjectAllList(hideDialog: Boolean) {
        rxHttpRequest {
            onRequest = {
                allProjectManager.value = RxHttp.get(NetUrl.GET_PROJECT_MANGER_ALL)
                    .toResponse<ProjectManager>().await()
            }
            loadingType = if (hideDialog) LoadingType.LOADING_NULL else LoadingType.LOADING_CUSTOM
        }
    }


    /**
     * 获取日、月报
     */
    fun requestAttendanceStat(type: Boolean, project_uuid: String? = "", search_date: String? = "", group_uuid: String? = "", is_list: String? = "") {
        rxHttpRequest {
            onRequest = {
                if (type) {
                    attendanceEntity.value = RxHttp.get(NetUrl.GET_ATTENDANCE_DAY)
                        .add("project_uuid", project_uuid)
                        .add("group_uuid", group_uuid)
                        .add("search_date", search_date)
                        .add("is_list", is_list)
                        .toResponse<AttendanceEntity>().await()
                } else {
                    attendanceEntity.value = RxHttp.get(NetUrl.GET_ATTENDANCE_MONTH)
                        .add("project_uuid", project_uuid)
                        .add("group_uuid", group_uuid)
                        .add("search_date", search_date)
                        .add("is_list", is_list)
                        .toResponse<AttendanceEntity>().await()
                }
            }
        }
    }


}